<?php
/*
	[gunianwang system]
	NAME:agency_control
	AUTHOR:hl
	DATE:2012-07-02
*/
if (!defined('IN_ELS'))
{
    die('Hacking attempt');
}
class control extends home_bannerbase {
	var $pagecount 		= 0;
	var $currendnum 	= 1;
	var $pageSize 		= 15;	//每页多少条
	var $pageNumSize 	= 10;	//分页数
	
	function control() {
		$this->home_bannerbase();
		if(!empty($_REQUEST['currendnum'])){
			$this->currendnum=$_REQUEST['currendnum'];
		}
		$act=empty($_REQUEST['a']) ? 'empty' : $_REQUEST['a'];
		
		if($act=="empty")				$this->bannerList();
		if($act=="banneradd")			$this->bannerEdit();
		if($act=="banneredit")			$this->bannerEdit();
		if($act=="bannerSave")			$this->bannerSave();
		if($act=="bannerdel") 			$this->bannerDel();
		if($act=="bannerdelall") 		$this->bannerDelall();
	}  
	function bannerList(){
		// news List
		$where = "where 1=1";
		$url = "";
		$dataCount=$this->getbannerCount($where);
		$startNum = $this->pageSize*($this->currendnum-1);
		$limitText = " limit ".$startNum." , ".$this->pageSize;
		$pagecountdata=array();
		require_once ELS_ROOT.'lib/paging.class.php';
		$pg =  new paging();
		$pg -> setLinkText("<a href='admin.php?p=home_banner$url&currendnum=!!PageNum!!'>!!LinkText!!</a>");
		$pg -> setCurrendnum(  $this-> currendnum);
		$pg -> setPageSize(    $this-> pageSize);
		$pg -> setPageNumSize( $this-> pageNumSize);
		$pg -> setDataCount(   $dataCount);
		$pagecountdata = $pg ->makePageText();
		
		$list=$this->getbannerList($where,$limitText);
		//print_r($list);exit;
		$this->view->assign('list',$list);
		$this->view->assign('pagecount' ,$pagecountdata);
		$this->view->assign('currendnum',$currendnum);
		
		$this->view->display("admin_homebannerlist");
	}
	//banner添加修改初始页面
	function bannerEdit(){
		if($_REQUEST['t']=='modify'){
			$id  = $_REQUEST['id'];
			$data=$this->getbannerContent($id);
			$this->view->assign('data' ,$data);
			$this->view->assign('active_type' ,'modify');
		}else{
			$data['is_ok'] = 1; 
			$this->view->assign('data' ,$data);
			$this->view->assign('active_type' ,'add');
		}
		$this->view->display("admin_homebanneredit");
	}
	//banner添加入库操作
	function bannerSave(){
	//print_r($_REQUEST);exit;
		$active_type	= $_REQUEST['active_type'];
		$title   		= $_REQUEST["title"];
		$url   			= $_REQUEST["url"];
		$u_id   		= $_REQUEST["user_id"];
		$is_ok 			= empty($_REQUEST['is_ok'])?1:intval($_REQUEST['is_ok']);
		$path 			= $_FILES["image_url"]['name'];
		
		$hasError = false;
		if($active_type == "add"){
			if($path == "" ){
				$this->view->assign('path_err'  ,"请上传图片");
				$hasError = true;
			}
		}
		
		$name1 = gmdate('Ym');
		$name2 = gmdate('j');
		//创建稿件文件夹
		$file_path = 'data/upImage/home_banner/'.$name1;
		if(!@file_exists($file_path)){ 
			@mkdir($file_path,   0777);
		}
		$file_path_dir = 'data/upImage/home_banner/'.$name1.'/'.$name2.'/';
		if(!@file_exists($file_path_dir)){ 
			@mkdir($file_path_dir,   0777);
		}
		$file_err = 'path_err';
		$picture_name = $this->uploadImage($path,$_FILES['image_url'],$file_path_dir,'paths_err');
		
		//有错误
		if($picture_name == 1){
			$this->view->assign('path_err'  ,"图片上传失败！");
			$hasError = true;
		}
		
		$dataArray=array();
		$dataArray['title'] 				= trim($title);
		$dataArray['url'] 					= trim($url);
		$dataArray['u_id'] 					= trim($u_id);
		$dataArray['is_ok'] 				= $is_ok;
		if($title == "" ){
			$this->view->assign('errtitle'  ,"名称不能为空");
			$hasError = true;
		}
		if($url == ""){
			$this->view->assign('errurl'  ,"必须填写连接地址");
			$hasError = true;
		}elseif (!eregi("http://[^\s]*",$url) && !empty($url) ) {
			//该链接格式不正确
			$this->view->assign('errurls'  ,"请填写正确的链接地址!");
			$hasError = true;
		}
		//如果有错误,提示错误消息并结束
		if($hasError){
			//如果已上传图片删除掉
			if($picture_name != ""){
				$this->deleteFile(ELS_ROOT.$file_path_dir.$picture_name);
			}
			
			//如果是修改信息
			if($active_type == "modify"){
				$id 		= 	$_REQUEST['id'];
				$data=$this->getabannerContent($id);
				$dataArray = $data;
			}
			
			$this->view->assign('active_type' ,$active_type);
			$this->view->assign('data' ,$dataArray);
			$this->view->display("admin_homebanneredit");
			exit;
		}
		//添加信息状态
		if($_REQUEST['active_type']=='add'){
			$dataArray['image_url']		= $name1.'/'.$name2.'/'.$picture_name;	//图片路径入库
			$dataArray['last_admin'] 			= $_SESSION["admin"]['uid'];//操作人
			$dataArray['create_time'] 	= $this->getCurrTimeLine();//创建时间
			$this->insertbanner($dataArray);
		}else{//修改信息状态
			$id = empty($_REQUEST['id'])?0:$_REQUEST['id'];
			//根据id查询信息
			$data = $this->getbannerContent($id);
			
			//如果上传图片判断以前有没有上传过图片，有就更新掉；
			if(!empty($path)){
				if(!empty($data["image_url"])){
					$this->deleteFile(ELS_ROOT.'./data/upImage/home_banner/'.$data["image_url"]);//删除图片
				}
				$dataArray['image_url'] = $name1.'/'.$name2.'/'.$picture_name;
			}

			$dataArray['last_admin'] 			= $_SESSION["admin"]['uid'];//操作人
			$where ="id=".$id;
			$this->updatebanner($dataArray,$where);
		}
		$this->pageRedirect("admin.php?p=home_banner");
	
	}
	//合作机构删除操作
	function bannerDel(){
		$id = empty($_REQUEST['id'])?0:$_REQUEST['id'];
		
		$data = $this->getbannerContent($id);
		if(!empty($data["image_url"])){
			$this->deleteFile(ELS_ROOT.'data/upImage/home_banner/'.$data["image_url"]);
		}

		$this->db->query("DELETE FROM gnw_banner WHERE id = '$id'");
		
		$this->pageRedirect("?p=home_banner");
	}
	//合作机构批量删除
	function bannerDelall(){
		$id= empty($_POST['ids'])?array():$_POST['ids'];
		//删除
		//print_r($id);exit;
		if($id){
			for($i=0;$i<count($id);$i++){
				$data[$i] = $this->getbannerContent($id[$i]);
				if(!empty($data[$i]["image_url"])){
					$this->deleteFile(ELS_ROOT.'data/upImage/home_banner/'.$data[$i]["image_url"]);
				}
				
				$this->db->query("DELETE FROM gnw_banner WHERE id = '$id[$i]'");
			}
		}
		
		$this->pageRedirect("?p=home_banner");
	}
}
?>